<!--
 * @Author       : tg
 * @LastEditors  : wfl
 * @description  : 组织管理--组织列表
 * @updateInfo   :
 * @Date         : 2023-10-17 10:05:50
 * @LastEditTime: 2023-10-23 10:10:15
-->
<script lang="ts" setup name="OrganizationTable">
import { _ } from 'iking-utils'
import type { Ref } from 'vue'
import { useFields } from './fields'
import { organizaApi } from '@/MainApp/apis/organiza'

const props = defineProps({
  // 编辑数据状态
  isEditing: {
    type: Boolean,
    default: false
  }
})
const { msgError, msgBoxWarnAsync, msgSuccess } = useMessage()
const selectDepNode: Ref<any> | undefined = inject('selectDepNode')
const { fieldList } = useFields()
const router = useRouter()
watch(
  () => selectDepNode?.value,
  val => {
    if (val)
      handChild()
    if (props.isEditing)
      handleCreate()
  },
  { deep: true }
)

const handDepTree = async () => {
  // const { success, msg, data } = await depApi.getDepTree()
}
handDepTree()

const depData = ref([])
const pageData = ref()
const totalNum = ref(0)
const handChild = async () => {
  const { success, msg, data, total } = await organizaApi.getChildDep({ ...pageData.value, id: selectDepNode?.value?.id })
  success && (depData.value = data)
  totalNum.value = total
  if (!success) {
    msgError(msg)
    depData.value = []
  }
  depData.value = _.cloneDeep(depData.value)
}
// 操作方法
const lookOrgan = (name: any, row: any) => {
  // 员工花名册
  if (name === 'staff') {
    router.push(
      {
        path: '/system-manage/organization-manage/StaffRosterDetailData',
        query: {
          id: row.id
        }
      }
    )
  }
  else {
    let title = ''
    // 无子级
    if (!row.hasChildren) {
      title = `停用【${row.name}】后，平台中历史数据正常显示，但新数据中将不会出现该组织。确定停用吗？`
    }
    else { // 有子级
      title = `【${row.name}】存在下级组织，请确保当前组织无下级组织后，再停用。可选择将下级组织移动到其他组织或停用。`
    }
    msgBoxWarnAsync(row.status === 'ACTIVE' ? '停用确认' : '启用确认', row.status === 'ACTIVE' ? title : `确认启用${row.name}吗？`, async () => {
      const { msg, success } = row.status === 'ACTIVE' ? await organizaApi.deactiveId(row.id) : await organizaApi.activeId(row.id)
      if (success) {
        msgSuccess(msg)
        handChild()
      }
      else { msgError(msg) }
    })
  }
}
// 新增组织
const handleCreate = () => {
  router.push(
    {
      path: '/system-manage/organization-manage/OrganizationModal',
      query: {
        treeId: selectDepNode?.value?.id, // 左侧treeid
        isEditing: props.isEditing ? 'edit' : 'add'
      }
    }
  )
}
</script>

<template>
  <IkPageFull
    v-model="pageData"
    :serial="false"
    :table-data="depData"
    :fields="fieldList"
    highlight-current-row
    header-border
    row-key="id"
    :total="totalNum"
    header-title="组织列表"
    :search="handChild"
  >
    <template #headerRight>
      <el-button type="primary" @click="handleCreate">新增组织</el-button>
    </template>
    <template #grade="{ data }">
      {{ data.row.grade === 1 ? '一级' : data.row.grade === 2 ? '二级' : data.row.grade === 3 ? '三级' : data.row.grade === 4 ? '四级' : data.row.grade === 5 ? '五级' : '六级' }}
    </template>
    <template #status="{ data }">
      <IkStatu
        :type="data.row.status === 'ACTIVE' ? 'success' : 'warning'"
        :title="data.row.status === 'ACTIVE' ? '启用' : '停用'"
      />
    </template>
    <template #controls="{ data }">
      <IkSvgIcon
        show-bg
        pop
        in-table
        :title="data.row.status === 'ACTIVE' ? '停用' : '启用'"
        :name="data.row.status === 'ACTIVE' ? 'iksvg_jinyong' : 'iksvg_qiyong'"
        :type="data.row.status === 'ACTIVE' ? 'danger' : 'success'"
        @click="lookOrgan('status', data.row)"
      />
      <IkSvgIcon
        show-bg
        pop
        in-table
        title="员工花名册"
        name="iksvg_huamingce"
        @click="lookOrgan('staff', data.row)"
      />
    </template>
  </IkPageFull>
</template>
